package com.im.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.manager.ConversationManager;
import com.im.app.model.*;
import com.im.app.model.User;
import com.im.app.processor.ChatMessageProcessor;
import com.im.app.service.*;
import com.im.app.utils.*;
import com.im.app.utils.uuid.IdUtils;
import com.im.app.utils.uuid.RSAUtils;
import org.apache.commons.collections4.map.LinkedMap;
import org.jim.core.ImPacket;
import org.jim.core.cache.redis.JedisTemplate;
import org.jim.core.packets.*;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/preForm")
public class PreFormController {

    @Resource
    private IPreFormService preFormService;
    @Resource
    private IPersonhostService personhostService;


    @GetMapping(value = "/queryPreFormList")
    @RequireLogin
    public ResponseResult<Page<PreForm>> queryPreFormList(@CurrentUser User user, PreForm preForm, int currPage, int pageSize) {
        //调用业务层返回列表
        preForm.setKefuId(user.getUserId());
        Page<PreForm> page = preFormService.selectPreFormList(preForm,currPage,pageSize);
        return Response.makeOKRsp(page);
    }


    @GetMapping(value ="/find")
    @RequireLogin
    public ResponseResult<PreForm> findPreForm(Integer id) {
        try {
            //调用业务层返回列表
            PreForm preForm = preFormService.selectPreFormById(Long.valueOf(id));
            return Response.makeOKRsp(preForm);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.makeErrRsp("查询异常");
        }
    }

    @GetMapping(value ="/delete")
    @RequireLogin
    public ResponseResult deletePreForm(@CurrentUser User user , Integer id) {
        if(user == null){
            return Response.makeErrRsp("用户不存在");
        }
        int result = preFormService.deletePreFormById(Long.valueOf(id));
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }

    }

    @PostMapping(value ="/add")
    public ResponseResult<Object> addPreForm(PreForm preForm, HttpServletRequest request) {
        preForm.setIp(IpUtils.getIpAddr(request));
        preFormService.insertPreForm(preForm);
        return Response.makeOKRsp();
    }

    @GetMapping(value = "/getFormConfig")
    @RequireLogin
    public ResponseResult<Object> getFormConfig(@CurrentUser User user) {
        FormConfig formConfig = preFormService.selectConfigByUserId(user.getUserId());
        return Response.makeOKRsp(formConfig);
    }

    @GetMapping(value = "/getConfig")
    public ResponseResult<Object> getConfig(String code,int type) {
        Personhost personhost = null;
        if(type == 1){
            personhost=personhostService.getPersonhostByCode(code);
        }else if(type == 2) {
            //独立域名
            String domain = "http://" + code;
            personhost=personhostService.getPersonHostByDomain(domain);
        }
        if (personhost == null){
            return Response.makeErrRsp("当前链接已失效");
        }
        FormConfig formConfig = preFormService.selectConfigByUserId(personhost.getUserId());
        return Response.makeOKRsp(formConfig);
    }

    @PostMapping(value = "/editForm")
    @RequireLogin
    public ResponseResult<Object> editForm(@CurrentUser User user,@RequestBody FormConfig config) {

        FormConfig formConfig = preFormService.selectConfigByUserId(user.getUserId());
        config.setUserId(user.getUserId());
        if (StringUtils.isEmpty(config.getConfig())){
            return Response.makeErrRsp("表单内容不为能空");
        }
        if (formConfig == null){
            preFormService.insertConfig(config);
        }else {
            config.setId(formConfig.getId());
            preFormService.updateFormConfig(config);
        }
        return Response.makeOKRsp(config);
    }

    @PostMapping(value ="/update")
    @RequireLogin
    public ResponseResult updatePreForm(@CurrentUser User user,@RequestBody PreForm preForm) {
        preForm.setKefuId(user.getUserId());
        int result = preFormService.updatePreForm(preForm);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }



}
